package com.wannengbang.cloudleader.service;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.CardListBean;
import com.wannengbang.cloudleader.bean.ConsumePlanBean;
import com.wannengbang.cloudleader.dialog.ChooseCardDialog;
import com.wannengbang.cloudleader.event.CardEvent;
import com.wannengbang.cloudleader.event.PlanEvent;
import com.wannengbang.cloudleader.service.model.IServiceModel;
import com.wannengbang.cloudleader.service.model.ServiceModelImpl;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.ViewLoading;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddPlanActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_card)
    TextView tvCard;
    @BindView(R.id.ll_card)
    LinearLayout llCard;
    @BindView(R.id.edit_amount)
    EditText editAmount;
    @BindView(R.id.edit_count_min)
    EditText editCountMin;
    @BindView(R.id.edit_count_max)
    EditText editCountMax;
    @BindView(R.id.tv_start_date)
    TextView tvStartDate;
    @BindView(R.id.ll_start_date)
    LinearLayout llStartDate;
    @BindView(R.id.tv_end_date)
    TextView tvEndDate;
    @BindView(R.id.ll_end_date)
    LinearLayout llEndDate;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private Calendar selectedDate;
    private int timeType = 1;
    private String card_id = "";
    private IServiceModel serviceModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_plan);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        card_id = getIntent().getStringExtra("card_id");
        serviceModel = new ServiceModelImpl();
        selectedDate = Calendar.getInstance();
    }

    public void requestPlanAdd(String card_id, String amount, String consume_count_min, String consume_count_max, String start_date, String end_date) {
        serviceModel.requestPlanAdd(card_id, amount, consume_count_min, consume_count_max, start_date, end_date, new DataCallBack<ConsumePlanBean>() {
            @Override
            public void onSuccessful(ConsumePlanBean bean) {
                ToastUtil.showShort("创建成功");
                EventBus.getDefault().post(new PlanEvent());
                Intent intent = new Intent(mActivity, PlanDetailsActivity.class);
                intent.putExtra("plan_id", bean.getData().getPlan_id());
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.ll_card, R.id.ll_start_date, R.id.ll_end_date, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_card:
                ChooseCardDialog dialog = ChooseCardDialog.newInstance();
                dialog.show(getSupportFragmentManager(), "dialog");
                dialog.setOnCallBackListener((name, chooseId) -> {
                    tvCard.setText(name);
                    card_id = chooseId;
                });
                break;
            case R.id.ll_start_date:
                timeType = 1;
                selectTime();
                break;
            case R.id.ll_end_date:
                timeType = 2;
                selectTime();
                break;
            case R.id.tv_commit:
//                if (TextUtils.isEmpty(card_id)) {
//                    ToastUtil.showShort("请选择信用卡");
//                    return;
//                }
                String amount = editAmount.getText().toString();
                if (TextUtils.isEmpty(amount)) {
                    ToastUtil.showShort("请输入账单金额");
                    return;
                }
//                String count_min = editCountMin.getText().toString();
//                if (TextUtils.isEmpty(count_min)) {
//                    ToastUtil.showShort("请输入消费笔数");
//                    return;
//                }
//                String count_max = editCountMax.getText().toString();
//                if (TextUtils.isEmpty(count_max)) {
//                    ToastUtil.showShort("请输入消费笔数");
//                    return;
//                }
                String start_date = tvStartDate.getText().toString();
                if (TextUtils.isEmpty(start_date)) {
                    ToastUtil.showShort("请选择计划开始日期");
                    return;
                }
                String end_date = tvEndDate.getText().toString();
                if (TextUtils.isEmpty(end_date)) {
                    ToastUtil.showShort("请选择计划结束日期");
                    return;
                }
                ViewLoading.showProgress(mActivity, "提交中......");
                String amount1 = NumberFormatUtils.formatDouble(Double.parseDouble(amount) * 100);
                requestPlanAdd(card_id, amount1, "", "", start_date, end_date);
                break;
        }
    }

    private TimePickerView pvTime;

    public void selectTime() {
        //这里可以更改参数，有好多类型
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        startDate.set(1900, 0, 1);
        endDate.set(2099, 11, 31);
        pvTime = new TimePickerBuilder(mActivity, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String time = DateTimeUtil.format(date);
                if (timeType == 1) {
                    tvStartDate.setText(time);
                }
                if (timeType == 2) {
                    tvEndDate.setText(time);
                }
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setSubmitColor(getResources().getColor(R.color.text_color_2))//确定按钮文字颜色
                .setSubCalSize(14)
                .setCancelColor(getResources().getColor(R.color.text_color_2))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(R.color.line_color_gray))
                .setTextColorCenter(getResources().getColor(R.color.text_color_2))
                .setContentTextSize(15)
                .setLineSpacingMultiplier(2)
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window window = mDialog.getWindow();
            if (window != null) {
                window.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.BOTTOM;
            }
        }
        pvTime.show();
    }
}
